import React,{ Component,Fragment} from 'react';
import messageMC from '../static/css/message.module.css';
import Message from './Message';
import CommonC from '../static/css/common.css';

class ShowMessage extends Component{
  constructor(props){
    super(props);
    this.state = {
      sendState:'loading'
    };
  }
  render(){
    let messageList = [];   //封装后的消息列表
    let oneDom = [];
    if(this.props.message){
      let message = JSON.parse(JSON.stringify(this.props.message));
      for(let i in message){
        oneDom = null;;
        if(message[i]['type'] == 'tip'){
          oneDom = <li className={messageMC.tipMsg} key={i}>
                    <Message center="true" msg={message[i]['text']} />
                  </li>
        }
        else if(message[i]['type'] == 'message'){
          if(message[i]['oriention'] == 'other'){
            oneDom = <li className={messageMC.otherMsg} key={i}>
                      <Message left="true" msg={message[i]['text']} name={message[i]['title']} />
                     </li>
          }
          if(message[i]['oriention'] == 'self'){

            oneDom = <li className={messageMC.myMsg} key={i}>
                      <Message right="true" msg={message[i]['text']} states={message[i]['states']} />
                     </li>
          }
        }
        messageList.push(oneDom);
      }
    }
    return(
      <Fragment>
        <ul ref="msgList" className={messageMC.msgList}>
          {messageList}
          <div className="clear"></div>
        </ul>
      </Fragment>
    )
  }
  shouldComponentUpdate(newProps, newState){
    let message = newProps['message'];
    if(message){
      return true;
    }
    else{
      return false;
    }
  }
  componentDidUpdate(prevProps, prevState){
    let message = JSON.parse(JSON.stringify(this.props.message));
    let newest = message.pop();
    if(newest && newest['oriention'] == 'self'){
      let ulHeight = this.refs.msgList.clientHeight;
      this.props.changeScroll(ulHeight);
    }
    // return true;
  }
}

export default ShowMessage;